<template>
  <div>
    <el-dialog
      :title="flag.isAdd?'用户添加':'用户修改'"
      :visible.sync="flag.isShow"
      @closed="closed"
      @opened="opened"
    >
      <el-form :model="form">
        {{form}}
        <el-form-item label="所属角色" label-width="70px">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option
              :label="item.rolename"
              :value="item.id"
              v-for="item in roleList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="70px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="70px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
          <div v-if="!flag.isAdd">如果不更改密码则为原密码</div>
        </el-form-item>
        <el-form-item label="状态" label-width="70px">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag.isShow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="flag.isAdd">添加</el-button>
        <el-button type="primary" @click="change" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { userAdd, userEdit } from "../../../request/api";
export default {
  data() {
    return {
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1
      }
    };
  },
  props: ["flag", "user"],
  computed: {
    ...mapGetters({
      userCount: "manage/userCount",
      userList: "manage/userList",
      userInfo: "manage/userInfo",
      roleList: "manage/roleList"
    })
  },
  components: {},
  methods: {
    ...mapActions({
      reqUserCount: "manage/reqUserCount",
      reqUserList: "manage/reqUserList",
      reqUserInfo: "manage/reqUserInfo"
    }),
    add() {
      this.flag.isShow = false;
      userAdd(this.form);
      this.clear();
      this.reqUserList(this.user);
      this.reqUserCount();
    },
    change() {
      if (!this.form.password) {
        this.form.password = this.userInfo.password;
      }
      userEdit(this.form)
      this.flag.isShow = false
      this.reqUserList(this.user)
      this.clear()
    },
    closed() {},
    opened() {
      if (!this.flag.isAdd) {
        this.form.username = this.userInfo.username;
        this.form.roleid = this.userInfo.roleid;
        this.form.uid = this.userInfo.uid;
      }
    },
    clear() {
      this.form = { roleid: "", username: "", password: "", status: 1 };
    }
  },
  mounted() {}
};
</script>
<style lang="less" scoped>
</style>